<div class="layui-card">
    <div class="layui-card-header">
        <h2 class="header-title">考生成绩与志愿信息</h2>
        <span class="layui-breadcrumb pull-right">
          <a href="#!console">首页</a>
          <a><cite>考生成绩与志愿信息</cite></a>
        </span>
    </div>
    <div class="layui-card-body">
        <div class="layui-form toolbar">
            搜索：<select id="ache-vol-search-key">
            <option value="">-请选择-</option>
            <option value="ksh">考生号</option>
            <option value="xm">姓名</option>
            <option value="jhlb">计划类别</option>
        </select>
            <input id="ache-vol-search-value" class="layui-input search-input" type="text" placeholder="输入关键字"/>&emsp;
            <button id="ache-vol-btn-search" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
        </div>

        <table class="layui-table" id="ache-vol-table" lay-filter="ache-vol-table"></table>
    </div>
</div>


<!-- 表格操作列 -->
<script type="text/html" id="ache-vol-tab-bar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="export">
        <span class="layui-icon layui-icon-export" title="导出"></span>
    </a>
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="details">
        <span class="layui-icon layui-icon-about" title="详情"></span>
    </a>
</script>

<script type="text/html" id="ache-vol-tab-toolbar">
    <a class="layui-btn layui-btn-primary layui-btn-md" lay-event="exportWord" title="批量导出Word">
        导出Word
    </a>
    <a class="layui-btn layui-btn-primary layui-btn-md" lay-event="exportExcel" title="批量导出Excel">
        导出Excel
    </a>
</script>

<script>
    layui.use(['form', 'table', 'util', 'config', 'admin', 'excel'], function () {
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var layer = layui.layer;
        var util = layui.util;
        var admin = layui.admin;
        let excel = layui.excel;

        // 渲染表格
        var renderTable = function () {
            table.render({
                elem: '#ache-vol-table',
                // url: config.base_server + 'user_query.json',
                url: config.base_server + 'api-firebird/fireBird/getAcheAndVolInfoList',
                method: 'GET',
                title: '成绩志愿表',
                where: {
                    access_token: config.getToken().access_token,
                },
                page: true,
                toolbar: '#ache-vol-tab-toolbar', //让工具栏左侧显示默认的内置模板
                cols: [[
                    {type: 'checkbox'},
                    {type: 'numbers'},
                    {field: 'candiNum', title: '考生号'},
                    {field: 'candiName', title: '姓名'},
                    {field: 'batch', title: '批次'},
                    {field: 'families', title: '科类'},
                    {field: 'planNature', title: '计划性质'},
                    {field: 'planCate', title: '计划类别'},
                    {field: 'admType', title: '录取类型'},
                    {field: 'score', title: '考生成绩', sort: true},
                    {align: 'center', toolbar: '#ache-vol-tab-bar', title: '操作', width: 150}
                ]]
            });
        };
        renderTable();
        // 工具条点击事件
        table.on('tool(ache-vol-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'details') { // 查看表字段
                showEditModel(data);
            } else if (layEvent === 'export') {
                // console.log(data);
                layer.load();
                admin.req('api-firebird/fireBird/exportAcheVolInfo', JSON.stringify(data), function (res) {
                    layer.closeAll('loading');
                    layer.alert(res.msg);
                }, 'POST');
                // var url = config.base_server + 'api-firebird/fireBird/exportAcheVolInfo?access_token='+config.getToken().access_token;
                // var xhr = new XMLHttpRequest();
                // xhr.open('POST', url, true);
                // xhr.responseType = "blob";
                // xhr.setRequestHeader("client_type", "DESKTOP_WEB");
                // xhr.onload = function() {
                //     if (this.status == 200) {
                //         var blob = this.response;
                //         var objecturl = URL.createObjectURL(blob);
                //         window.location.href = objecturl;
                //     }
                // }
                // xhr.send();
            }
        });

        //显示表单弹窗
        var showEditModel = function (data) {
            admin.putTempData('t_ache_vol_info', data);
            var title = data.candiNum + " - " + data.candiName + " - 成绩与志愿信息";
            admin.popupCenter({
                title: title,
                path: 'pages/business/achevol_details.html',
                area: ['800px', '740px'],
                offset: [0, 0],
                scrollbar: true
            });
        };

        table.on('toolbar(ache-vol-table)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            let layEvent = obj.event;
            let data = checkStatus.data;
            switch (layEvent) {
                case 'exportWord':
                    layer.load();
                    if (data.length <= 0) {
                        admin.req('api-firebird/fireBird/getAcheAndVolInfoList', {}, function (res) {
                            data = res.data;
                        }, 'GET');
                    }
                    admin.req('api-firebird/fireBird/exportAcheVolInfo', JSON.stringify(data), function (res) {
                        layer.closeAll('loading');
                        layer.alert(res.msg);
                    }, 'POST');
                    break;
                case 'exportExcel':
                    // let thead = ['考生号', '姓名', '批次', '科类', '计划性质', '计划类别',
                    //     '投档单位', '录取类型', '考生状态', '考生成绩', '专业投档基准成绩', '备注'];
                    let thead = [{
                        candiNum: '考生号', candiName: '姓名', batch: '批次', families: '科类',
                        planNature: '计划性质', planCate: '计划类别', pitchUnit: '投档单位', admType: '录取类型',
                        exmStatus: '考生状态', score: '考生成绩', profBenchmarkScore: '专业投档基准成绩', note: '备注'
                    }];
                    //如果未选中数据，则视为导出全部数据
                    if (data.length <= 0) {
                        admin.req('api-firebird/fireBird/getAcheAndVolInfoList', {}, function (res) {
                            data = res.data;
                        }, 'GET');
                    }
                    let filterdata = ["candiNum", "candiName", "batch", "families", "planNature",
                        "planCate", "pitchUnit", "admType", "exmStatus", "score", "profBenchmarkScore", "note"
                    ];
                    data = excel.filterExportData(data, filterdata);
                    thead.push.apply(thead, data);
                    excel.exportExcel(thead, '考生成绩与志愿信息表.xlsx', 'xlsx');
                    // table.exportFile(thead,data, 'xls');
                    break;
            }
        })

        $("#ache-vol-btn-search").click(function () {
            let key = $("#ache-vol-search-key").val();
            let value = $("#ache-vol-search-value").val();
            if (key!='ksh')
                value = value + '%';
            table.reload('ache-vol-table', {where: {searchKey: key,searchValue: value}})
        });
    });

</script>